function photoUpdateDescription(sender)
{
	sender.defaultValue = sender.value;
}

function getNextTag(tag, after)
{
	var el = after.nextSibling;
	while (el && el.nodeName != tag)
	{
		el = el.nextSibling;
	}
	if (el && el.nodeName == tag)
		return el;
	else
		return null;
};

function getPrevTag(tag, before)
{
	var el = before.previousSibling;
	while (el && el.nodeName != tag)
	{
		el = el.previousSibling;
	}
	if (el && el.nodeName == tag)
		return el;
	else
		return null;
};

function photoTop(sender)
{
	var current = sender.parentNode.parentNode;
	var prev = getPrevTag('DIV',current);
	while (prev != null)
	{
		var tmp = current.innerHTML;
		current.innerHTML = prev.innerHTML;
		prev.innerHTML = tmp;
		current = prev;
		prev = getPrevTag('DIV',current);
	}
};

function photoUp(sender)
{
	var current = sender.parentNode.parentNode;
	var prev = getPrevTag('DIV',current);
	if (prev != null)
	{
		var tmp = current.innerHTML;
		current.innerHTML = prev.innerHTML;
		prev.innerHTML = tmp;
	}
};

function photoDown(sender)
{
	var current = sender.parentNode.parentNode;
	var next = getNextTag('DIV',current);
	if (next != null)
	{
		var tmp = current.innerHTML;
		current.innerHTML = next.innerHTML;
		next.innerHTML = tmp;
	}
};

function photoBottom(sender)
{
	var current = sender.parentNode.parentNode;
	var next = getNextTag('DIV',current);
	while (next != null)
	{
		var tmp = current.innerHTML;
		current.innerHTML = next.innerHTML;
		next.innerHTML = tmp;
		current = next;
		next = getNextTag('DIV',current);
	}
};

function photoRemove(sender)
{
	
	if(confirm('Do you really want to delete this page?'))
	sender.parentNode.parentNode.parentNode.removeChild(sender.parentNode.parentNode);
};

function photoAdd(galleryId, id, link, description)
{
	var galDiv = document.getElementById(galleryId);
	if (galDiv) {
		var galChildren = galDiv.getElementsByTagName('div');
		var galActual = null;
		if (galChildren && galChildren.length > 0) {
			for (var i = 0; i< galChildren.length; i++) {
				if (galChildren[i].className == 'photos-actual') {
					galActual = galChildren[i];
					break;
				}
			}
		}
		if (galActual) {
			var newItem = document.createElement('DIV');
			newItem.innerHTML = '<div class="photo-info"> '+
									'<a class="photo-preview"> '+
										'<span><img style="width: 300px;" src="'+link+'"/></span> '+
									'</a> '+
									'<span class="photo-link">'+link+'</span> '+
									'<input type="hidden" class="photo-link" value="'+link+'"/>'+
								'</div> '+
								'<div class="photo-select"> '+
									'<input type="hidden" class="photo-id" value="'+id+'"> '+
									'<input type="text" class="photo-description" value="'+description+'" onkeyup="photoUpdateDescription(this);"> '+
								'</div> '+
								'<div class="photo-move"> '+
									'<span onclick="photoUp(this);"><img src="/Content/Admin/Images/admin-right-up1.png"/></span> '+
									'<span onclick="photoDown(this);"><img src="/Content/Admin/Images/admin-right-down1.png"/></span> ' +
									'&nbsp;&nbsp; '+
									'<span onclick="photoTop(this);"><img src="/Content/Admin/Images/admin-right-uptop.png"/></span> ' +
									'<span onclick="photoBottom(this);"><img src="/Content/Admin/Images/admin-right-downbottom.png"/></span> ' +
								'</div> '+
								'<div class="photo-remove"> '+
									'<img onclick="photoRemove(this);" src="/Content/Admin/Images/admin-photo-remove.png"> ' +
								'</div> ';
			newItem.className = 'photo-item';
			galActual.appendChild(newItem);
		}
	}
	return false;
};

function photoAddUploader(galleryId, pageId, galleryDatabaseId, swfObjectPath)
{
	var galDiv = document.getElementById(galleryId);
	if (galDiv) {
		var galChildren = galDiv.getElementsByTagName('div');
		var galActual = null;
		if (galChildren && galChildren.length > 0) {
			for (var i = 0; i< galChildren.length; i++) {
				if (galChildren[i].className == 'photos-upload') {
					galActual = galChildren[i];
					break;
				}
			}
		}
		if (galActual) {
			var containerDiv = document.createElement('DIV');
			containerDiv.className = 'photo-item';
			galActual.appendChild(containerDiv);
			var placeholderDiv = document.createElement('DIV');
			placeholderDiv.id = 'uploader'+Guid();
			containerDiv.appendChild(placeholderDiv);
			
			var flashvars = {
								'galleryId' : galleryId,
								'containerId' : placeholderDiv.id,
								'pageId' : pageId,
								'galleryDatabaseId' : galleryDatabaseId
							};
			var params = {
							'id' : placeholderDiv.id,
							'name' : placeholderDiv.id,
							'allowscriptaccess' : 'always'
						};
            swfobject.embedSWF(swfObjectPath, placeholderDiv.id, "728", "25", "9.0.0", "", flashvars, params);
		}
	}
};

function submitPrepared(form) 
{
	if (form)
	{
		prepPhotos();
		form.submit();
	}
};

function photosRemoveUploader(containerId)
{
	var el = document.getElementById(containerId);
	if (el)
		el.parentNode.parentNode.removeChild(el.parentNode);
};

function prepPhotos()
{
	var allDivs = document.getElementsByTagName('div');
	for (var i = 0; i < allDivs.length; i++)
	{
		var div = allDivs[i];
		if (div && div.className == 'photo-all')
		{
			var postInput = document.createElement('input');
			postInput.setAttribute('name', div.id);
			var postValue='';
			
			var items = div.getElementsByTagName('div');
			for (var k=0;k<items.length;k++)
			{
				var item = items[k];
				if (item.className == 'photo-item')
				{
					var inputs = item.getElementsByTagName('input');
					if (inputs.length > 1)
					{
						var photoId = '';
						var photoDesc = '';
						var photoLink = '';
						for (var j = 0; j<inputs.length; j++)
						{
							var input = inputs[j];
							if (input)
							{
								if (input.className == 'photo-id')
									photoId = input.value;
								else if (input.className == 'photo-description')
									photoDesc = encodeURIComponent(input.value);
								else if (input.className == 'photo-link')
									photoLink = encodeURIComponent(input.value);
							}
						}
						postValue += photoId + ',' + photoDesc + ',' + photoLink + ';';
					}
				}
			}
			postInput.setAttribute('value', postValue);
			div.appendChild(postInput);
		}
	}
};

function Guid() {
	return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4()).toLowerCase();
};

function S4() {
	return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};

function showMessage(text) {
	alert(text);
}